# -*- coding: utf-8 -*-
#
# 系统 :管理员,权限,角色相关模型
#
#
from django.db import models
from .resource import Resource
from public_util.util.base import (BaseModelMixin,
                                   CacheAttribute,
                                   CachedClassAttribute)
import time, datetime, urllib
import traceback


class Menu(models.Model, BaseModelMixin):
    """
    菜单模型
    """
    parent_id = models.IntegerField('父节点id', default=0)
    name = models.CharField('名称', max_length=50, db_index=True, unique=True)
    url = models.CharField('访问地址带参数', max_length=400, blank=True, default='')
    url_path = models.CharField('访问路径', max_length=100, null=False, db_index=True)
    icon = models.CharField('图标', max_length=100, null=True, blank=True, default='')
    css = models.CharField("样式", max_length=100, null=True, blank=True, default='')
    order = models.IntegerField('排序', default=0)
    is_show = models.SmallIntegerField('显示', default=1)
    is_log = models.SmallIntegerField('记录日志', default=0)

    # objects = CacheManager

    def __unicode__(self):
        return '%s' % self.name

    @classmethod
    def url_decode(cls, url_params):
        '''URL参数返回一个字典
        '''
        params = {}
        for o in url_params.split('&'):
            if '=' in o:
                kv = o.split('=')
                if len(kv) != 2:
                    continue
                k, v = kv
                # params[k] = urllib.parse.unquote_plus(v.encode('utf-8')).decode('utf-8')
                params[k] = urllib.parse.unquote_plus(v)
        return params

    def get_url_params(self):
        '''返回url的参数字典
        '''
        url_params = self.url.split('?', 2)
        if len(url_params) == 2:
            return Menu.url_decode(url_params[1])
        return {}

    def is_match_url_parmas(self, menu_params):
        return self._is_dict_issubset(self.get_url_params(), menu_params)

    def _is_dict_issubset(self, dict1, dict2):
        list1 = []
        list2 = []
        map(lambda x: list1.append((x[0], tuple(x[1]))), dict1.items())
        map(lambda x: list2.append((x[0], tuple(x[1]))), dict2.items())
        # set1 = set(tuple(dict1.items()))
        # set2 = set(tuple(dict2.items()))
        set1 = set(list1)
        set2 = set(list2)
        return set1.issubset(set2)

    def save(self, *arge, **kwargs):
        self.url_path = self.url.split('?', 2)[0]
        if not self.is_show:
            self.css += 'color:gray;'
        super(Menu, self).save(*arge, **kwargs)

    class Meta:
        db_table = u'menu'
        ordering = ('order',)


class UserDefinedMenu(models.Model):
    ''' 用户自定义菜单模型 '''
    admin_id = models.IntegerField()
    defined_menu = models.TextField(u"用户定义的菜单项", max_length=1000)
    map_menu = models.TextField(u"系统菜单的映射", max_length=1000)
    update_time = models.DateTimeField()

    def __unicode__(self):
        return self.id

    class Meta:
        db_table = u"custom_menu"
        # app_label = "analys"


Resource.register('menu', Menu)


